package cn.ctodb.cms.service;

import cn.ctodb.cms.domain.CmsTxt;
import cn.ctodb.cms.repository.CmsTxtRepository;
import cn.ctodb.cms.repository.search.CmsTxtSearchRepository;
import cn.ctodb.cms.service.dto.CmsTxtDTO;
import cn.ctodb.cms.service.mapper.CmsTxtMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing CmsTxt.
 */
@Service
@Transactional
public class CmsTxtService {

    private final Logger log = LoggerFactory.getLogger(CmsTxtService.class);
    
    @Inject
    private CmsTxtRepository cmsTxtRepository;

    @Inject
    private CmsTxtMapper cmsTxtMapper;

    @Inject
    private CmsTxtSearchRepository cmsTxtSearchRepository;

    /**
     * Save a cmsTxt.
     *
     * @param cmsTxtDTO the entity to save
     * @return the persisted entity
     */
    public CmsTxtDTO save(CmsTxtDTO cmsTxtDTO) {
        log.debug("Request to save CmsTxt : {}", cmsTxtDTO);
        CmsTxt cmsTxt = cmsTxtMapper.cmsTxtDTOToCmsTxt(cmsTxtDTO);
        cmsTxt = cmsTxtRepository.save(cmsTxt);
        CmsTxtDTO result = cmsTxtMapper.cmsTxtToCmsTxtDTO(cmsTxt);
        cmsTxtSearchRepository.save(cmsTxt);
        return result;
    }

    /**
     *  Get all the cmsTxts.
     *  
     *  @return the list of entities
     */
    @Transactional(readOnly = true) 
    public List<CmsTxtDTO> findAll() {
        log.debug("Request to get all CmsTxts");
        List<CmsTxtDTO> result = cmsTxtRepository.findAll().stream()
            .map(cmsTxtMapper::cmsTxtToCmsTxtDTO)
            .collect(Collectors.toCollection(LinkedList::new));

        return result;
    }

    /**
     *  Get one cmsTxt by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Transactional(readOnly = true) 
    public CmsTxtDTO findOne(Long id) {
        log.debug("Request to get CmsTxt : {}", id);
        CmsTxt cmsTxt = cmsTxtRepository.findOne(id);
        CmsTxtDTO cmsTxtDTO = cmsTxtMapper.cmsTxtToCmsTxtDTO(cmsTxt);
        return cmsTxtDTO;
    }

    /**
     *  Delete the  cmsTxt by id.
     *
     *  @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete CmsTxt : {}", id);
        cmsTxtRepository.delete(id);
        cmsTxtSearchRepository.delete(id);
    }

    /**
     * Search for the cmsTxt corresponding to the query.
     *
     *  @param query the query of the search
     *  @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<CmsTxtDTO> search(String query) {
        log.debug("Request to search CmsTxts for query {}", query);
        return StreamSupport
            .stream(cmsTxtSearchRepository.search(queryStringQuery(query)).spliterator(), false)
            .map(cmsTxtMapper::cmsTxtToCmsTxtDTO)
            .collect(Collectors.toList());
    }
}
